﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.ViewportAdapters;
using MonoGame.Extended;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MonoGame.Extended.VectorDraw;

namespace MonoEarth.Entities
{
    public abstract class QGeometry : IDisposable, IEquatable<QGeometry>
    {
        protected PrimitiveDrawing _primitiveDrawing;
        protected PrimitiveBatch _primitiveBatch;
        protected Matrix _localProjection;
        protected Matrix _localView;
        protected readonly GameEarth _game;
        protected bool _isInitialized;
        protected SpriteBatch _spriteBatch;

        public QGeometry(GameEarth game)
        {
            _game = game;

        }
        public abstract string Name { get; set; }
        protected bool IsMouseVisible
        {
            get => _game.IsMouseVisible;
            set => _game.IsMouseVisible = value;
        }
        protected ContentManager Content { get; private set; }
        protected GameWindow Window => _game.Window;
        protected GraphicsDevice GraphicsDevice => _game.GraphicsDevice;
        protected GameComponentCollection Components => _game.Components;

        protected OrthographicCamera Camera { get; private set; }
        protected ViewportAdapter ViewportAdapter { get; private set; }
        protected virtual void Initialize()
        {
            ViewportAdapter = _game.ViewportAdapter;
           
            Camera = new OrthographicCamera(ViewportAdapter);
        }

        public abstract void LoadContent();
     
        public virtual void UnloadContent() { }
        public abstract void Update(GameTime gameTime);
        public abstract void Draw(GameTime gameTime) ;

        protected void Exit()
        {
        }
        private void InitContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _primitiveBatch = new PrimitiveBatch(GraphicsDevice);
            _primitiveDrawing = new PrimitiveDrawing(_primitiveBatch);
            _localProjection = Matrix.CreateOrthographicOffCenter(0f, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0f, 0f, 1f);
            _localView = Matrix.Identity;
        }
        public void Load()
        {
            if (!_isInitialized)
            {
                Initialize();
                _isInitialized = true;
            }

            if (Content == null)
            {
                Content = new ContentManager(_game.Services, "Content");
                InitContent();
                LoadContent();
            }
        }

        public void Unload()
        {
            if (Content != null)
            {
                Components.Clear();
                UnloadContent();
                Content.Unload();
                Content.Dispose();
                Content = null;
            }
        }
        public void OnUpdate(GameTime gameTime)
        {
            Update(gameTime);
        }

        public void OnDraw(GameTime gameTime)
        {
            Draw(gameTime);
        }
        public int CompareTo(QGeometry other)
        {
            return string.Compare(GetType().Name, other.GetType().Name, StringComparison.Ordinal);
        }

        public virtual void Dispose()
        {

        }

        public bool Equals(QGeometry other)
        {
            return string.Compare(GetType().Name, other.GetType().Name, StringComparison.Ordinal) == 0;
        }
    }
}
